package com.software.controller;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.plexus.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.software.dao.impl.MessageDetailDaoImpl;
import com.software.model.DrmAgentImg;
import com.software.model.DrmAgentIntroduce;
import com.software.model.DrmArticles;
import com.software.model.DrmCodes;
import com.software.model.DrmCourse;
import com.software.model.DrmUser;
import com.software.model.PageModel;
import com.software.service.AgentService;
import com.software.service.ImgMngService;
import com.software.service.IntroduceService;
import com.software.service.StuMngService;
import com.software.utils.Constants;

@Controller("homePageController")
@RequestMapping(value = "/agentHomePage/*")
public class AgentHomePageController extends BaseController{

	@Autowired
	private StuMngService stuMngService;
	
	@Autowired
	private MessageDetailDaoImpl mongo;
	
	@Autowired
	private AgentService agentService;
	
	@Autowired
	private IntroduceService introduceService;
	
	@Autowired
	private ImgMngService imgMngService;
	

	
	
	/**
	 * 代理商web init
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "homeInit", method = RequestMethod.GET)
	public ModelAndView init(HttpServletResponse res, HttpServletRequest req){
		ModelAndView view = new ModelAndView();
		PageModel vo = new PageModel();
		vo.setArticleTp(Constants.ARTICLE_TP_1);
		vo.setOrderKey("article_cdt");
		vo.setOrderKeySort("desc");
		vo.setIds(super.getUserIdInSession(req).toString());
		// 咨询list
		List<DrmArticles> list = agentService.searchInfoHomePage(vo);
		view.addObject("infoList", list);
		
		vo.setArticleTp(Constants.ARTICLE_TP_10);
		// 活动list
		List<DrmArticles> actList = agentService.searchInfoHomePage(vo);
		
	    view.addObject("actList", actList);
		
		DrmAgentIntroduce data = new DrmAgentIntroduce();
		// TODO 
		data.setAgentId(super.getUserIdInSession(req));
		DrmAgentIntroduce webData = introduceService.queryIntroduce(data);
		if(null != webData) {
			webData.setIntroduceTxt(Constants.TXT_URL + webData.getIntroduceTxt());
		}
		// 分中心数据
		view.addObject("centerData",  webData);
		
		DrmUser user = new DrmUser();
		user.setUserTp(Constants.USER_TP_2);
		user.setUserSourceid(super.getUserIdInSession(req));
		// 课程数据
		List<DrmCourse> courseList = stuMngService.searchCourseList(user);
		Map<String, String> codeMap = new LinkedHashMap<String,String>();
		for(DrmCourse course :courseList) {
			course.setCoursePimg(StringUtils.isNotBlank(course.getCoursePimg()) ? Constants.IMG_URL +  course.getCoursePimg() : "");
			codeMap.put(course.getCourseTpId().toString(), course.getCourseTpName());
		}
		
		List<DrmCodes> codeList = new ArrayList<DrmCodes>();
		for (Map.Entry<String, String> map : codeMap.entrySet()) {
			
			DrmCodes code = new DrmCodes();
			code.setCode(map.getKey());
			code.setName(map.getValue());
			codeList.add(code);
		}
		view.addObject("codeList",  codeList);
		view.addObject("courseList",  courseList);
		
		// 活动list
		List<DrmAgentImg> imgList = imgMngService.search5ImgList(vo);
		view.addObject("imgList", imgList);
		
		view.setViewName("agent/agency_web");
		
		return	view;
	}
	
	/**
	 *  咨询详细 显示
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "infoDetail", method = RequestMethod.GET)
	public ModelAndView infodetails(String id, HttpServletResponse res, HttpServletRequest req){
		ModelAndView view = new ModelAndView();
		// 咨询查询
		DrmArticles info = agentService.searchInfoDetails(id);
		if(null != info) {
			info.setArticleContent(Constants.TXT_URL + info.getArticleContent());
		}
		if(null != info) {
			info.setArticleImage(Constants.TXT_URL + info.getArticleImage());
		}	
		
		PageModel vo = new PageModel();
		vo.setArticleTp(Constants.ARTICLE_TP_1);
		vo.setOrderKey("article_cdt");
		vo.setOrderKeySort("desc");
		vo.setIds(super.getUserIdInSession(req).toString());
		// 咨询list
		List<DrmArticles> list = agentService.searchInfoHomePage(vo);
		
		view.addObject("infoList",  list.size() > 10 ? list.subList(0,10) : list);
		
		view.addObject("info", info);
		
		view.setViewName("agent/agency_webinfor");
		
		return	view;
	}
	
	
	/**
	 * 咨询list
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "infolist", method = RequestMethod.GET)
	public ModelAndView infolist(HttpServletResponse res, HttpServletRequest req){
		ModelAndView view = new ModelAndView();
		PageModel vo = new PageModel();
		vo.setArticleTp(Constants.ARTICLE_TP_1);
		vo.setOrderKey("article_cdt");
		vo.setOrderKeySort("desc");
		vo.setIds(super.getUserIdInSession(req).toString());
		// 咨询list
		List<DrmArticles> list = agentService.searchInfoHomePage(vo);
		
		view.addObject("infoList",  list);
		
		view.setViewName("agent/agency_weblist");
		
		return	view;
	}
	
	/**
	 * 活动list
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "actlist", method = RequestMethod.GET)
	public ModelAndView actlist(HttpServletResponse res, HttpServletRequest req){
		ModelAndView view = new ModelAndView();
		PageModel vo = new PageModel();
		vo.setArticleTp(Constants.ARTICLE_TP_10);
		vo.setOrderKey("article_cdt");
		vo.setOrderKeySort("desc");
		vo.setIds(super.getUserIdInSession(req).toString());
		// 咨询list
		List<DrmArticles> list = agentService.searchInfoHomePage(vo);
		
		view.addObject("infoList",  list);
		
		view.setViewName("agent/agency_webact");
		
		return	view;
	}
	
	
	/**
	 *  活动详细 显示
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "actdetail", method = RequestMethod.GET)
	public ModelAndView actdetails(String id, HttpServletResponse res, HttpServletRequest req){
		ModelAndView view = new ModelAndView();
		// 活动查询
		DrmArticles info = agentService.searchInfoDetails(id);
		if(null != info) {
			info.setArticleContent(Constants.TXT_URL + info.getArticleContent());
		}
		if(null != info) {
			info.setArticleImage(Constants.TXT_URL + info.getArticleImage());
		}		
		
		PageModel vo = new PageModel();
		vo.setArticleTp(Constants.ARTICLE_TP_10);
		vo.setOrderKey("article_cdt");
		vo.setOrderKeySort("desc");
		vo.setIds(super.getUserIdInSession(req).toString());
		// 咨询list
		List<DrmArticles> list = agentService.searchInfoHomePage(vo);
		
		view.addObject("actList",  list.size() > 10 ? list.subList(0,10) : list);
		
		view.addObject("act", info);
		
		view.setViewName("agent/agency_weblistcont");
		
		return	view;
	}
	
	/**
	 * 中心介绍 详细web
	 * @param imgData
	 * @param res
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "webCenterdetail",produces = "text/html;charset=UTF-8")
	public ModelAndView webCentInit (HttpServletResponse res, HttpServletRequest req){
		DrmAgentIntroduce imgData =new DrmAgentIntroduce();
		imgData.setAgentId(super.getUserIdInSession(req));
		DrmAgentIntroduce data = introduceService.queryIntroduce(imgData);
		data.setIntroduceTxt(StringUtils.isNotBlank(data.getIntroduceTxt()) ? Constants.TXT_URL + data.getIntroduceTxt() : "");
		
		data.setBanner(StringUtils.isNotBlank(data.getBanner()) ? Constants.IMG_URL + data.getBanner() : "");
		data.setBannerB(StringUtils.isNotBlank(data.getBannerB()) ? Constants.IMG_URL + data.getBannerB() : "");
		ModelAndView map = new ModelAndView();
		map.addObject("introduceData",  data);
		map.setViewName("agent/agency_webint");
		return	map;
	}
	
	
}